package com.ssyx.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssyx.common.result.Result;
import com.ssyx.model.product.Category;
import com.ssyx.product.service.CategoryService;
import com.ssyx.vo.product.CategoryQueryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:CategoryController
 * Description:
 *
 * @Author kay
 * @Create 2024/4/27 23:37
 * @Version 1.0
 */
@Tag(name = "商品分类管理")
@RestController
@RequestMapping("/admin/product/category")
//@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Operation(summary = "获取商品分类分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        CategoryQueryVo categoryQueryVo){
        Page<Category> pageParams = new Page<>(page,limit);
        IPage<Category> pageModel = categoryService.getPage(pageParams,categoryQueryVo);
        return Result.ok(pageModel);
    }

    @Operation(summary = "获取指定商品的分类信息")
    @GetMapping("get/{id}")
    public Result getById(@PathVariable Long id){
        Category category = categoryService.getById(id);
        return Result.ok(category);
    }

    @Operation(summary = "新增商品分类")
    @PostMapping("save")
    public Result save(@RequestBody Category category){
        categoryService.save(category);
        return Result.ok(null);
    }

    @Operation(summary = "修改商品分类")
    @PutMapping("update")
    public Result updateById(@RequestBody Category category){
        categoryService.updateById(category);
        return Result.ok(null);
    }

    @Operation(summary = "删除商品分类")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id){
        categoryService.removeById(id);
        return Result.ok(null);
    }

    @Operation(summary = "根据id列表删除商品分类")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        categoryService.removeByIds(idList);
        return Result.ok(null);
    }

    @Operation(summary = "获取全部商品分类")
    @GetMapping("findAllList")
    public Result findAllList(){
       List<Category> list = categoryService.findAllList();
       return Result.ok(list);
    }
}
